package co.edu.usbcali.egrapico.vista.ambient;

import co.edu.usbcali.egrapico.vista.FrmPrincipal;

public class LPaneRestrictionMsg extends javax.swing.JPanel implements java.io.Serializable {

    private java.util.Vector figures;
    private java.util.Vector Undo;
    private boolean ban = false, sel;
    private java.awt.Dimension area;
    private java.awt.Component dragComponent;
    private int xAdjustment, xPresed;
    private int yAdjustment, yPresed;

    /**
     * Creates a new instance of LPane
     */
    public LPaneRestrictionMsg() {
        figures = new java.util.Vector();
        Undo = new java.util.Vector();
        area = new java.awt.Dimension(0, 0);
        setLayout(null);
        setBackground(new java.awt.Color(255, 255, 255));

        addMouseListener(new java.awt.event.MouseAdapter() {

            public void mousePressed(java.awt.event.MouseEvent evt) {
                LPMousePressed(evt);
            }

            public void mouseReleased(java.awt.event.MouseEvent evt) {
                LPMouseReleased(evt);
            }

            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LPMouseClicked(evt);
            }
        });
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {

            public void mouseMoved(java.awt.event.MouseEvent evt) {
                LPMouseMoved(evt);
            }
        });
    }
    //methods

    public void addMyListener() {

        addMouseListener(new java.awt.event.MouseAdapter() {

            public void mousePressed(java.awt.event.MouseEvent evt) {
                LPMousePressed(evt);
            }

            public void mouseReleased(java.awt.event.MouseEvent evt) {
                LPMouseReleased(evt);
            }

            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LPMouseClicked(evt);
            }
        });
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {

            public void mouseMoved(java.awt.event.MouseEvent evt) {
                LPMouseMoved(evt);
            }
        });
    }

    private void LPMouseMoved(java.awt.event.MouseEvent evt) {
        java.awt.Component c = findComponentAt(evt.getX(), evt.getY());
        if (c instanceof LabelIdeOpe) {
            setToolTipText(((LabelIdeOpe) c).getTextToolTip());
        } else if (c instanceof LabelMethod) {
            setToolTipText(((LabelMethod) c).getTextToolTip());
        } else {
            setToolTipText("Ambient");
        }
    }

    private void LPMouseClicked(java.awt.event.MouseEvent evt) {
        java.awt.Component c = findComponentAt(evt.getX(), evt.getY());
        if (evt.getButton() == java.awt.event.MouseEvent.BUTTON3) {
            if (c instanceof LabelIdeOpe) {
                ((LabelIdeOpe) c).mostrarMenu(evt.getComponent(), evt.getX(), evt.getY());
            } else if (c instanceof LabelMethod) {
                ((LabelMethod) c).mostrarMenu(evt.getComponent(), evt.getX(), evt.getY());
            }
        }
        if (evt.getClickCount() == 2) {

            if (c instanceof LabelIdeOpe) {
                if (((LabelIdeOpe) c).isCreate() == false) {
                    ((LabelIdeOpe) c).Ladicionar();
                } else {
                    ((LabelIdeOpe) c).mostrarPanel();
                }

            } else if (c instanceof LabelMethod) {
                if (((LabelMethod) c).isCreate() == false) {
                    ((LabelMethod) c).Ladicionar();
                } else {
                    ((LabelMethod) c).mostrarPanel();
                }
            }
        }
    }

    private void LPMousePressed(java.awt.event.MouseEvent evt) {
        sel = true;
        dragComponent = null;
        java.awt.Component c = findComponentAt(evt.getX(), evt.getY());
        if (c instanceof javax.swing.JPanel) {
            FrmPrincipal.setComment("Restrictions Ambient");
            return;
        }
        if (c instanceof LabelIdeOpe) {
            FrmPrincipal.setComment(((LabelIdeOpe) c).getTextPane());
        }
        if (c instanceof LabelMethod) {
            FrmPrincipal.setComment(((LabelMethod) c).getTextPane());
        }

        dragComponent = c;
        xAdjustment = dragComponent.getLocation().x - evt.getX();
        yAdjustment = dragComponent.getLocation().y - evt.getY();
        dragComponent.setLocation(evt.getX() + xAdjustment, evt.getY() + yAdjustment);
        if (evt.getButton() == java.awt.event.MouseEvent.BUTTON1) {
            ban = true;
        } else if (evt.getButton() == java.awt.event.MouseEvent.BUTTON3) {
            ban = false;
            sel = true;
            xPresed = dragComponent.getLocation().x + dragComponent.getWidth() / 2;
            yPresed = dragComponent.getLocation().y + dragComponent.getHeight() / 2;
        }
    }

    private void LPMouseReleased(java.awt.event.MouseEvent evt) {
        dragComponent = null;
        java.awt.Component c = findComponentAt(evt.getX(), evt.getY());
        if (!sel) {
            if (c instanceof LabelIdeOpe) {
                Forma myshape = (Forma) figures.get(figures.size() - 1);
                myshape.setPoint2(c.getX() + c.getWidth() / 2, c.getY() + c.getHeight() / 2);
            } else if (c instanceof LabelMethod) {
                Forma myshape = (Forma) figures.get(figures.size() - 1);
                myshape.setPoint2(c.getX() + c.getWidth() / 2, c.getY() + c.getHeight() / 2);
            } else {//if (c instanceof javax.swing.JPanel){
                if (figures.lastElement() instanceof Linea) {
                    figures.remove(figures.size() - 1);
                }
            }
        }        //Hacemos un solo Undo en el realease si el objeto no es un label objeto
        repaint();
    }

    public void paintComponent(java.awt.Graphics g) {
        super.paintComponent(g);
        int i = 0;
        for (int j = figures.size(); i < j; i++) {
            if (figures.get(i) instanceof Linea) {
                Forma myshape = (Forma) figures.get(i);
                myshape.draw(g);
            }
        }
    }

    public void addLabeltoVector(LabelIdeOpe labe) {
        figures.add(labe);
    }

    public void addLabeltoVector(LabelMethod labe) {
        figures.add(labe);
    }

    public void addLabel(LabelIdeOpe labe) {
        add(labe);
        revalidate();
        repaint();
    }

    public void Undo() {
        Undo.add(figures.lastElement());
        int i = figures.size();
        if (figures.get(i - 1) instanceof LabelIdeOpe) {//|| figures.get(i-1)instanceof LabelMethod){
            remove((java.awt.Component) figures.get(i - 1));
        }
        figures.remove(i - 1);
        repaint();
    }

    public int getUndoSize() {
        return Undo.size();
    }

    public void Redo() {
        if (Undo.lastElement() instanceof LabelIdeOpe || Undo.lastElement() instanceof LabelMethod) {
            add((java.awt.Component) Undo.lastElement());
        }
        figures.add(Undo.lastElement());
        int i = Undo.size();
        Undo.remove(i - 1);
        repaint();
    }

    public int getRedoSize() {
        return figures.size();
    }

    public java.awt.Dimension areaLPane() {
        return area;
    }

    public void setAreaLP(java.awt.Dimension ar) {
        area = ar;
    }
}